package org.qiuqiu.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.qiuqiu.pojo.Contact;
import org.qiuqiu.pojo.Group;
import org.qiuqiu.pojo.User;
import org.qiuqiu.tools.NameTools;
import org.qiuqiu.tools.SqlMapTool;

/**
 * @author 杜秋野
 * 
 */
public class ContactDao {

	/**
	 * 该方法为插入联系人的方法
	 * 
	 * @param contact
	 * @return
	 * @throws SQLException
	 */
	public boolean addContact(Contact contact) throws SQLException {
		boolean result = false;
		try {
			SqlMapTool.getSqlMap().startTransaction();
			SqlMapTool.getSqlMap().insert("insertContact", contact);
			SqlMapTool.getSqlMap().commitTransaction();
			result = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = false;
		} finally {
			SqlMapTool.getSqlMap().endTransaction();
		}
		return result;
	}

	/**
	 * 该方法修改联系人的资料
	 * 
	 * @param contact
	 * @return
	 * @throws SQLException
	 */
	public boolean modContact(Contact contact) throws SQLException {
		boolean result = false;
		try {
			SqlMapTool.getSqlMap().startTransaction();
			SqlMapTool.getSqlMap().update("modContact", contact);
			SqlMapTool.getSqlMap().commitTransaction();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
		} finally {
			SqlMapTool.getSqlMap().endTransaction();
		}
		return result;
	}

	/**
	 * 该方法删除指定的联系人
	 * 
	 * @param list
	 * @return
	 * @throws SQLException
	 */
	public boolean delContact(List<Integer> list) throws SQLException {
		boolean result = false;
		try {
			SqlMapTool.getSqlMap().startTransaction();
			SqlMapTool.getSqlMap().startBatch();
			for (Integer i : list) {
				SqlMapTool.getSqlMap().delete("delContact", i);
			}
			SqlMapTool.getSqlMap().executeBatch();
			SqlMapTool.getSqlMap().commitTransaction();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
		} finally {
			SqlMapTool.getSqlMap().endTransaction();
		}
		return result;
	}

	/**
	 * 该方法将移动用户到指定分组
	 * 
	 * @param list
	 * @param newGroup
	 * @return
	 * @throws SQLException
	 */
	public boolean moveContact(List<Integer> list, int newGroup)
			throws SQLException {
		boolean result = false;
		try {
			SqlMapTool.getSqlMap().startTransaction();
			SqlMapTool.getSqlMap().startBatch();
			for (int contactId : list) {
				Map<String, Integer> map = new HashMap<String, Integer>();
				map.put("newGroup", newGroup);
				map.put("contactId", contactId);
				SqlMapTool.getSqlMap().update("moveContact", map);
			}
			SqlMapTool.getSqlMap().executeBatch();
			SqlMapTool.getSqlMap().commitTransaction();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			SqlMapTool.getSqlMap().endTransaction();
		}
		return result;
	}

	/**
	 * 该方法根据参数不同返回某个用户的全部联系人信息或某个群组的联系人信息或者指定的某个联系人信息
	 * 前面三个参数一次只能有一个不为0,剩下的两个都必须为0,0表示查找的相关性 第四个参数表示是否倒序,0表示顺序,1表示倒序
	 * 
	 * @param userId
	 * @param groupId
	 * @param contactId
	 * @param order
	 * @return
	 * @throws SQLException
	 */
	public List<HashMap> getList(int userId, int groupId, int contactId,
			int order) throws SQLException {
		List<HashMap> list = new ArrayList<HashMap>();
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("userId", userId);
		map.put("groupId", groupId);
		map.put("contactId", contactId);
		map.put("order", order);
		list = SqlMapTool.getSqlMap().queryForList("getContact", map);
		return list;
	}

	public List<HashMap> instant(HashMap<String, String> hm)
			throws SQLException {
		List<HashMap> list = new ArrayList<HashMap>();
		list = SqlMapTool.getSqlMap().queryForList("instant", hm);
		return list;
	}

	public static void main(String args[]) {
		ContactDao cd = new ContactDao();
		try {
			List<HashMap> list = cd.getList(6, 0, 0, 0);
			System.out.println(list);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}